﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL

    Public Class Person
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal person_id As Integer, _
            ByVal client_id As Integer, _
            ByVal user_name As String, _
            ByVal first_name As String, _
            ByVal last_name As String, _
            ByVal title As String, _
            ByVal email_address As String, _
            ByVal phone_number_fax As String, _
            ByVal phone_number_home As String, _
            ByVal phone_number_mobile As String, _
            ByVal phone_number_office As String, _
            ByVal phone_number_office_ext As String, _
            ByVal identity_url As String, _
            ByVal im_handle As String, _
            ByVal im_service As String, _
            ByVal has_access_to_new_projects As Boolean, _
            ByVal administrator As Boolean, _
            ByVal deleted As Boolean, _
            ByVal uuid As Guid)


            Me.person_id = person_id
            Me.client_id = client_id
            Me.user_name = user_name
            Me.first_name = first_name
            Me.last_name = last_name
            Me.title = title
            Me.email_address = email_address
            Me.phone_number_fax = phone_number_fax
            Me.phone_number_home = phone_number_home
            Me.phone_number_mobile = phone_number_mobile
            Me.phone_number_office = phone_number_office
            Me.phone_number_office_ext = phone_number_office_ext
            Me.identity_url = identity_url
            Me.im_handle = im_handle
            Me.im_service = im_service
            Me.has_access_to_new_projects = has_access_to_new_projects
            Me.administrator = administrator
            Me.deleted = deleted
            Me.uuid = uuid

        End Sub

        ''' <summary>
        ''' Returns Person for API user from Basecamp API
        ''' </summary>
        Public Shared Function GetMe() As Person
            Return GetPersonFromPersonDetails(BasecampProvider.API.GetMe())
        End Function

        ''' <summary>
        ''' Returns Person from Basecamp API
        ''' </summary>
        Public Shared Function GetPerson(ByVal person_id As Integer) As Person
            Return GetPersonFromPersonDetails(BasecampProvider.API.GetPerson(person_id))
        End Function

        ''' <summary>
        ''' Returns a list of all people as List(Of Person) from Basecamp API
        ''' </summary>
        Public Shared Function GetPeople() As List(Of Person)
            Return GetPersonListFromPersonDetailsCollection(BasecampProvider.API.GetPeople())
        End Function

        ''' <summary>
        ''' Returns a list of all people associated with a project as List(Of Person) from Basecamp API
        ''' </summary>
        Public Shared Function GetPeopleByProject(ByVal project_id As Integer) As List(Of Person)
            Return GetPersonListFromPersonDetailsCollection(BasecampProvider.API.GetPeopleByProject(project_id))
        End Function

        ''' <summary>
        ''' Returns a list of all people associated with a company as List(Of Person) from Basecamp API
        ''' </summary>
        Public Shared Function GetPeopleByCompany(ByVal company_id As Integer) As List(Of Person)
            Return GetPersonListFromPersonDetailsCollection(BasecampProvider.API.GetPeopleByCompany(company_id))
        End Function

#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of Person) from List(Of PersonDetails)
        ''' </summary>
        Private Shared Function GetPersonListFromPersonDetailsCollection(ByVal recordset As List(Of PersonDetails)) As List(Of Person)
            Dim people As New List(Of Person)
            Dim record As PersonDetails = Nothing
            For Each record In recordset
                people.Add(GetPersonFromPersonDetails(record))
            Next
            Return people
        End Function

        ''' <summary>
        ''' Helper Function to create a Person from PersonDetails
        ''' </summary>
        Private Shared Function GetPersonFromPersonDetails(ByVal record As PersonDetails) As Person
            Return New Person(record.person_id, record.client_id, record.user_name, record.first_name, record.last_name, _
                          record.title, record.email_address, record.phone_number_fax, record.phone_number_home, record.phone_number_mobile, record.phone_number_office, record.phone_number_office_ext, _
                          record.identity_url, record.im_handle, record.im_service, record.has_access_to_new_projects, record.administrator, record.deleted, record.uuid)
        End Function

#End Region

#Region "Properties"

        Private _person_id As Integer
        Public Property person_id() As Integer
            Get
                Return _person_id
            End Get
            Set(ByVal value As Integer)
                _person_id = value
            End Set
        End Property

        Private _client_id As Integer
        Public Property client_id() As Integer
            Get
                Return _client_id
            End Get
            Set(ByVal value As Integer)
                _client_id = value
            End Set
        End Property

        Private _user_name As String
        Public Property user_name() As String
            Get
                Return _user_name
            End Get
            Set(ByVal value As String)
                _user_name = value
            End Set
        End Property


        Private _first_name As String
        Public Property first_name() As String
            Get
                Return _first_name
            End Get
            Set(ByVal value As String)
                _first_name = value
            End Set
        End Property


        Private _last_name As String
        Public Property last_name() As String
            Get
                Return _last_name
            End Get
            Set(ByVal value As String)
                _last_name = value
            End Set
        End Property

        Private _title As String
        Public Property title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property


        Private _email_address As String
        Public Property email_address() As String
            Get
                Return _email_address
            End Get
            Set(ByVal value As String)
                _email_address = value
            End Set
        End Property

        Private _phone_number_fax As String
        Public Property phone_number_fax() As String
            Get
                Return _phone_number_fax
            End Get
            Set(ByVal value As String)
                _phone_number_fax = value
            End Set
        End Property

        Private _phone_number_home As String
        Public Property phone_number_home() As String
            Get
                Return _phone_number_home
            End Get
            Set(ByVal value As String)
                _phone_number_home = value
            End Set
        End Property

        Private _phone_number_mobile As String
        Public Property phone_number_mobile() As String
            Get
                Return _phone_number_mobile
            End Get
            Set(ByVal value As String)
                _phone_number_mobile = value
            End Set
        End Property

        Private _phone_number_office As String
        Public Property phone_number_office() As String
            Get
                Return _phone_number_office
            End Get
            Set(ByVal value As String)
                _phone_number_office = value
            End Set
        End Property


        Private _phone_number_office_ext As String
        Public Property phone_number_office_ext() As String
            Get
                Return _phone_number_office_ext
            End Get
            Set(ByVal value As String)
                _phone_number_office_ext = value
            End Set
        End Property



        Private _identity_url As String
        Public Property identity_url() As String
            Get
                Return _identity_url
            End Get
            Set(ByVal value As String)
                _identity_url = value
            End Set
        End Property

        Private _im_handle As String
        Public Property im_handle() As String
            Get
                Return _im_handle
            End Get
            Set(ByVal value As String)
                _im_handle = value
            End Set
        End Property

        Private _im_service As String
        Public Property im_service() As String
            Get
                Return _im_service
            End Get
            Set(ByVal value As String)
                _im_service = value
            End Set
        End Property


        Private _has_access_to_new_projects As Boolean
        Public Property has_access_to_new_projects() As Boolean
            Get
                Return _has_access_to_new_projects
            End Get
            Set(ByVal value As Boolean)
                _has_access_to_new_projects = value
            End Set
        End Property

        Private _administrator As Boolean
        Public Property administrator() As Boolean
            Get
                Return _administrator
            End Get
            Set(ByVal value As Boolean)
                _administrator = value
            End Set
        End Property

        Private _deleted As Boolean
        Public Property deleted() As Boolean
            Get
                Return _deleted
            End Get
            Set(ByVal value As Boolean)
                _deleted = value
            End Set
        End Property


        Private _uuid As Guid
        Public Property uuid() As Guid
            Get
                Return _uuid
            End Get
            Set(ByVal value As Guid)
                _uuid = value
            End Set
        End Property



#End Region

    End Class

End Namespace